package org.boot.ssm.wxshop.service.impl;

import org.boot.ssm.wxshop.common.impl.BaseServiceImpl;
import org.boot.ssm.wxshop.domain.User;
import org.boot.ssm.wxshop.domain.UserLevel;
import org.boot.ssm.wxshop.exception.BusinessException;
import org.boot.ssm.wxshop.mapper.UserMapper;
import org.boot.ssm.wxshop.service.UserLevelService;
import org.boot.ssm.wxshop.service.UserService;
import org.boot.ssm.wxshop.util.Md5Utils;
import org.boot.ssm.wxshop.util.PhoneMsgCode;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 用户业务层实现类
 *
 * @author 刘涛
 * @date 2018/3/313
 */


@Service
public class UserSerivceImpl extends BaseServiceImpl<User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserLevelService userLevelService;

    @Override
    /*****************************
     * @Description: 登录名验证
     * @Param: [user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    public JsonResult verifyLoginName(User user) {
        Integer i = this.queryCount(user);
        if (i == 0) {
            return new JsonResult(200, "用户名可用", null);
        } else {
            return new JsonResult(500, "用户名重复", null);

        }
    }

    @Override
    /*****************************
     * @Description: 添加或修改用户
     * @Param: [user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult<java.lang.String>
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    public JsonResult addOrUpdateUser(User user) {
        user.setLoginpwd(Md5Utils.getMd5(user.getLoginpwd()));
        Integer isSuccess;
        if (user.getId() == null || user.getId().equals("")) {
            isSuccess = this.saveNoNull(user);
            if (isSuccess == 1) {
                return new JsonResult(200, "添加成功", "/user/goUserPage");
            } else {
                throw new BusinessException("添加失败");
            }
        } else {
            isSuccess = this.updateNoNull(user);
            if (isSuccess == 1) {
                return new JsonResult(200, "修改成功", "/user/goUserPage");
            } else {
                throw new BusinessException("修改失败");
            }
        }
    }

    @Override
    /*****************************
     * @Description: 逻辑删除用户
     * @Param: [user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    public JsonResult deleteUser(User user) {
        Integer isDelete;
        user.setIsDelete(0);
        isDelete = this.updateDelete(user);
        if (isDelete == 1) {
            return new JsonResult(200, "删除成功");
        } else {
            throw new BusinessException("删除失败");
        }
    }

    @Override
    /*****************************
     * @Description: 根据用户ID查询用户当前用户等级
     * @Param: [id]
     * @return: org.boot.ssm.wxshop.domain.User
     * @Author: 刘涛
     * @Date: 2018/3/14
     *******************************/
    public User selectUserAndLevel(Integer id) {
        return userMapper.selectUserAndLevel(id);
    }

    @Override
    /*****************************
     * @Description: 后台用户登陆
     * @Param: [session, user]
     * @return: java.lang.String
     * @Author: 刘涛
     * @Date: 2018/3/18
     *******************************/
    public JsonResult backstageLogin(User user) {
        user.setIsDelete(1);
        user.setLoginpwd(Md5Utils.getMd5(user.getLoginpwd()));
        User thrUser = this.queryByParam(user);
        if (thrUser != null) {
            UserLevel userLevel = userLevelService.queryById(thrUser.getUserLevelId());
            thrUser.setUserLevelName(userLevel.getName());
            return new JsonResult(200, "登陆成功", thrUser);
        } else {
            throw new BusinessException("账号或密码错误");
        }
    }

    @Override
    /*****************************
     * @Description: 重置密码
     * @Param: [user]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/18
     *******************************/
    public JsonResult resetPwd(User user) {
        Integer isReset;
        user.setLoginpwd(Md5Utils.getMd5(user.getLoginpwd()));
        isReset = this.updateNoNull(user);
        if (isReset == 1) {
            return new JsonResult(200, "重置成功");
        } else {
            throw new BusinessException("重置失败");
        }
    }


    /**
     * 修改密码
     * @param user
     * @param oldPwd
     * @param newPwd
     * @return
     */
    @Override
    public JsonResult editPwd(User user,String oldPwd,String newPwd) {
        if(user.getLoginpwd().equals(oldPwd)){
            user.setLoginpwd(newPwd);
            Integer isEdit = this.updateNoNull(user);
            if(isEdit==1){
                return new JsonResult(200,"修改成功");
            }else {
                throw new BusinessException("修改失败");
            }
        }else {
            throw new BusinessException("原密码不正确");
        }
    }


    /**
     * 前台用户注册
     *
     * @param user
     * @return
     */
    @Override
    public JsonResult registUser(User user) {
        String code = PhoneMsgCode.getOutId(user.getBizId(), user.getPhone());
        if (code != null) {
            System.out.println(code);
            if (code.equals(user.getOutId())) {
                user.setIsAdmin(0);
                user.setLoginpwd(Md5Utils.getMd5(user.getLoginpwd()));
                Integer isSave = this.saveNoNull(user);
                if (isSave == 1) {
                    return new JsonResult(200, "注册成功", user);
                } else {
                    throw new BusinessException("注册失败");
                }
            } else {
                throw new BusinessException("验证码错误");
            }
        } else {
            throw new BusinessException("验证码错误");
        }

    }

}
